package com.topscomm.mpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectProblemEntity;
import com.topscomm.mpm.service.impl.MpmProjectProblemService;
import com.topscomm.mpm.service.impl.MpmProjectService;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import com.topscomm.tap.TapScript;
import org.springframework.transaction.annotation.Transactional;


public class MpmProjectProblemScript extends TapScript {
    //状态enum 用流程节点名字来同步问题开放状态
    private enum NodeName{
        Start{
            public String toString(){
               return "开始" ;
            }
        }
        ,
        Finish{
            public String toString(){
                return "结束" ;
            }
        },
        Open{
            public String toString(){
                return "开放" ;
            }
        },
        Close{
            public String toString(){
                return "关闭" ;
            }
        }

        }
    /**
      * @description: 同步问题状态和审核状态
      * @author: xinfushuo
      * @param: 项目问题流程的service 和当前nodeins对应的项目问题entity
      * @throws:
      * @return:
      * @date: 2021/8/6
      * @modify:
      */
    @Transactional
    public void SyncProblemStatus(ITapService service, TapEntity entity)throws Exception{

        CboApprovalNodeInsEntity nodeinsentity = (CboApprovalNodeInsEntity) entity;
        MpmProjectProblemService problemservice =(MpmProjectProblemService) SpringUtil.getBean("mpmProjectProblemService");
        MpmProjectProblemEntity problementity = problemservice.queryById(ConvertUtil.convertToString(nodeinsentity.getSourceid()));
        //  problem status 与nodeins字段extendfield2 绑定
        //判空 枚举 逻辑注释
        //若当前问题状态为审核中,且尚未走到结束节点
        if(problementity.getState() == 1 && !nodeinsentity.getExtendfield2().contentEquals(NodeName.Finish.toString())) {
            problementity.setProblemstatus(NodeName.Open.toString());
        }else {
            problementity.setProblemstatus(NodeName.Close.toString());
        }
        problemservice.updateEntity(problementity);
    }
    /**
      * @description:同步项目最新项目经理信息
      * @author: xinfushuo
      * @param:项目问题流程的service 和当前nodeins对应的项目问题entity
      * @throws:
      * @return:
      * @date: 2021/8/9
      * @modify: 8.16 更正之前使用projectcode作为projectid的错误
      */
    @Transactional
    public void syncProjectInfo(ITapService service, TapEntity entity)throws Exception{
        //获取项目entity和关联的Projec的Entity
        CboApprovalNodeInsEntity nodeInsEntity = (CboApprovalNodeInsEntity) entity;
        MpmProjectProblemService problemService =(MpmProjectProblemService) SpringUtil.getBean("mpmProjectProblemService");
        MpmProjectProblemEntity problemEntity = problemService.queryById(ConvertUtil.convertToString(nodeInsEntity.getSourceid()));
        MpmProjectService projectService =(MpmProjectService)SpringUtil.getBean("mpmProjectService");
        MpmProjectEntity projectEntity  = projectService.queryById(ConvertUtil.convertToString(problemEntity.getProjectid()));
        //获取当前项目经理和工号,不匹配则更新
        boolean managerChanged  = !projectEntity.getProjectmanagercode().contentEquals(problemEntity.getProjectmanagercode()) ;
        boolean levelChanged = !projectEntity.getProjectlevel().contentEquals(problemEntity.getProjectlevel());
        //项目经理信息变更
        if(managerChanged){
            problemEntity.setProjectmanager(projectEntity.getProjectmanager());
            problemEntity.setProjectmanagercode(projectEntity.getProjectmanagercode());
        }
        //项目等级变更
        if(levelChanged){
            problemEntity.setProblemlevel(projectEntity.getProjectlevel());
        }
        //任意一个变更则更新
        if(managerChanged||levelChanged){
            problemService.updateEntity(problemEntity);
        }
    }
}
